package com.longge;

import java.util.ArrayList;
import java.util.List;

public class FindMode {

    private List<Integer> nums = new ArrayList<>();
    int cur = Integer.MIN_VALUE, count = 0, max = 0;

    public int[] findMode(TreeNode root) {

        dfs(root);


        return nums.stream().mapToInt(i -> i).toArray();
    }


    private void dfs(TreeNode root) {
        if (root == null) return;
        dfs(root.left);
        if (cur != root.val) {
            cur = root.val;
            count = 1;
        } else {
            count++;
        }
        if (max == count) {
            nums.add(root.val);
        } else if (max < count) {
            nums.clear();
            nums.add(root.val);
            max = count;
        }
        dfs(root.right);

    }
}
